﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Web;
using System.Security.Cryptography;

namespace BartonExtension
{
    public static class StringExtensions
    {
        public static string FormatWith(this string formatStr,params object[] parameters)
        {
            return string.Format(formatStr,parameters);
        }

        public static bool isNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

        public static string ToJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj).Replace(@"\", "") ;
        }

        public static T FromJson<T>(this string jsonString)
        {
            if (jsonString.isNullOrEmpty())
                return default(T);
            T TObject = JsonConvert.DeserializeObject<T>(jsonString);
            return TObject;
        }

        public static string DateTimeFileName(this DateTime datetime)
        {
            string str = DateTime.Now.ToString("yyyyMMddhhmmssfffffff");
            return str;
        }

        public static string HtmlDecode(this string str)
        {
            return HttpUtility.HtmlDecode(str);
        }

        public static string UrlDecode(this string str)
        {
            return HttpUtility.UrlDecode(str);
        }

        public static string UrlEncode(this string str)
        {
            return HttpUtility.UrlEncode(str);
        }

        public static string toMD5(this string str)
        {
            // 创建MD5类的默认实例：MD5CryptoServiceProvider
            MD5 md5 = MD5.Create();
            byte[] bs = Encoding.UTF8.GetBytes(str);
            byte[] hs = md5.ComputeHash(bs);
            StringBuilder sb = new StringBuilder();
            foreach (byte b in hs)
            {
                // 以十六进制格式格式化
                sb.Append(b.ToString("x2"));
            }
            return sb.ToString();
        }

        public static string HumanReadableDateInDay(this DateTime date)
        {
            string[] humanReadableMonth = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
            string month = humanReadableMonth[date.ToString("MM").toInt32().Value];
            return month + " " + date.ToString("dd") + "号";
        }

        public static string ArrayToSplitedString(this List<string> list,string spliter)
        {
            string str = string.Empty;
            list.ForEach(l =>
                {
                    if (list.Last().Equals(l))
                        str += l;
                    else
                        str += l + spliter;
                });
            return str;
        }

        public static List<string> SplitedStringToArray(this string str,string spliter)
        {
            return str.Split(new string[] { spliter }, StringSplitOptions.None).ToList();
        }
    }
}
